<template>
  <div class="depth-carousel">
    <el-button class="nav-btn left" @click="prev" circle>
      <el-icon>
        <ArrowLeft />
      </el-icon>
    </el-button>
    <div class="carousel-track">
      <div v-for="(item, idx) in items" :key="idx" class="carousel-card" :style="cardStyle(idx)"
        :class="{ active: idx === current }">
        <img v-if="item.img" :src="item.img" :alt="item.title || ''" />
        <div v-else class="card-content">
          <slot :item="item">{{ item.title }}</slot>
        </div>
      </div>
    </div>
    <el-button class="nav-btn right" @click="next" circle>
      <el-icon>
        <ArrowRight />
      </el-icon>
    </el-button>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ArrowLeft, ArrowRight } from '@element-plus/icons-vue'

const props = defineProps({
  items: { type: Array, required: true }, // [{img, title}]
  visible: { type: Number, default: 5 },  // 同时显示卡片数，建议奇数
})

const current = ref(0)
const len = props.items.length

const prev = () => {
  current.value = (current.value - 1 + len) % len
}
const next = () => {
  current.value = (current.value + 1) % len
}

const cardStyle = (idx) => {
  const mid = Math.floor(props.visible / 2)
  let offset = (idx - current.value + len) % len
  if (offset > len / 2) offset -= len // 处理循环
  if (Math.abs(offset) > mid) return { opacity: 0, pointerEvents: 'none', transform: 'scale(0.7) translateX(0)' }
  // 景深、缩放、位移、透明度
  return {
    zIndex: props.visible - Math.abs(offset),
    opacity: 1 - Math.abs(offset) * 0.18,
    transform: `translateX(${offset * 60}px) scale(${1 - Math.abs(offset) * 0.12})`,
    filter: offset === 0 ? 'drop-shadow(0 8px 32px #6366f1aa)' : 'blur(0.5px)',
    transition: 'all 0.5s cubic-bezier(.34,1.56,.64,1)',
  }
}
</script>

<style lang="scss" scoped>
.depth-carousel {
  position: relative;
  width: 480px;
  height: 260px;
  margin: 0 auto;
  display: flex;
  align-items: center;

  .carousel-track {
    position: relative;
    width: 100%;
    height: 220px;
    display: flex;
    align-items: center;
    justify-content: center;
    perspective: 1000px;
  }

  .carousel-card {
    position: absolute;
    left: 50%;
    top: 0;
    width: 220px;
    height: 220px;
    border-radius: 18px;
    overflow: hidden;
    background: #fff;
    box-shadow: 0 4px 24px rgba(80, 80, 180, 0.10);
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;

    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
      border-radius: 18px;
      user-select: none;
      pointer-events: none;
    }

    .card-content {
      padding: 24px;
      font-size: 1.2rem;
      color: #333;
      text-align: center;
    }

    &.active {
      border: 2px solid #6366f1;
      box-shadow: 0 8px 40px #6366f1;
    }
  }

  .nav-btn {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    z-index: 10;

    &.left {
      left: -48px;
    }

    &.right {
      right: -48px;
    }
  }
}
</style>